<template>
  <BasicModal v-bind="$attrs" :showFooter="true" @register="registerModal" @ok="handleSubmit" width="600px">
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="ViewsTbTenantGroupForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { TenantGroup, getTenantGroupById, tenantGroupSave } from '/@/api/tb/tenantGroup';
  import { EntityType } from '/@/enums/entityTypeEnum';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('tb');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<TenantGroup>({} as TenantGroup);
  const getTitle = computed(() => ({
    icon: meta.icon || 'ant-design:appstore-outlined',
    value: record.value.id?.id ? '编辑租户分组' : '新增租户分组',
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      field: 'id.entityType',
      component: 'Input',
      show: false,
      componentProps: {
        value: EntityType.TENANT_GROUP,
      },
    },
    {
      label: '分组名称',
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 100,
        placeholder: '请输入分组名称',
      },
      required: true,
      colProps: { lg: 24, md: 24 },
    },
    {
      label: '分组描述',
      field: 'description',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
        rows: 4,
        placeholder: '请输入分组描述',
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, getFieldsValue, validate }] = useForm({
    labelWidth: 100,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ loading: true });
    record.value = { ...data } as TenantGroup;
    await resetFields();
    if (data?.id?.id) {
      const res = await getTenantGroupById(data.id.id);
      record.value = (res || {}) as TenantGroup;
    }
    setFieldsValue(record.value);
    setModalProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setModalProps({ confirmLoading: true });
      // 如果是新增,移除 id 字段
      if (!record.value.id?.id) {
        delete data.id;
      } else {
        // 如果是编辑,确保 id 包含完整信息
        data.id = record.value.id;
      }
      await tenantGroupSave(data);
      showMessage(`${record.value.id?.id ? '编辑' : '新增'}租户分组成功！`);
      closeModal();
      emit('success');
    } catch (error: any) {
      console.log(error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
